﻿using MediatR;
using ThreeTeams.API.Application.Command.ProductCommand;
using ThreeTeams.Domain.ThreeTeams.Domain;
using ThreeTeams.ErrorCode;
using ThreeTeams.Infrastructure;

namespace ThreeTeams.API.Application.CommandHandler.ProductCommandHandler
{
    public class DeleteProductCommandHandler : IRequestHandler<DeleteProductCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Product> baseRepository;

        public DeleteProductCommandHandler (IBaseRepository<Product> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public async Task<ApiResult<int>> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var result= new ApiResult<int>();
            var id = request.Ids.Split(',').Select(int.Parse).ToList();
            var entity=await baseRepository.DeletedRange(p => id.Contains(p.Id));              
            if (entity > 0)
            {
                result.Code = ApiEnum.Ok;
                result.Message = "删除成功";
            }
            else
            {
                result.Code = ApiEnum.Fail;
                result.Message = "删除失败";
            }
            return result;
        }
    }
}
